﻿namespace Foggy.Data
{
    using System;

    using Caliburn.Micro;

    /// <summary>
    /// Represents an issue
    /// </summary>
    public class Issue : PropertyChangedBase
    {
        /// <summary>
        /// The issue unique identifier
        /// </summary>
        private int id;

        /// <summary>
        /// The issue title
        /// </summary>
        private string title;

        private string project;

        private string priorityName;

        /// <summary>
        /// The original estimate
        /// </summary>
        private TimeSpan? originalEstimate;

        /// <summary>
        /// The current estimate
        /// </summary>
        private TimeSpan? currentEstimate;

        /// <summary>
        /// The current amount of time spent on the issue
        /// </summary>
        private TimeSpan? elapsedTime;        

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>
        /// The id.
        /// </value>
        public int Id
        {
            get
            {
                return this.id;
            }

            set
            {
                this.id = value;
                this.NotifyOfPropertyChange(() => this.Id);
            }
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>
        /// The title.
        /// </value>
        public string Title
        {
            get
            {
                return this.title;
            }

            set
            {
                this.title = value;
                this.NotifyOfPropertyChange(() => this.Title);
            }
        }

        public string Project
        {
            get
            {
                return this.project;
            }

            set
            {
                this.project = value;
                this.NotifyOfPropertyChange(() => this.Project);
            }
        }

        public string PriorityName
        {
            get
            {
                return this.priorityName;
            }

            set
            {
                this.priorityName = value;
                this.NotifyOfPropertyChange(() => this.PriorityName);
            }
        }

        /// <summary>
        /// Gets or sets the original estimate.
        /// </summary>
        /// <value>
        /// The original estimate.
        /// </value>
        public TimeSpan? OriginalEstimate
        {
            get
            {
                return this.originalEstimate;
            }

            set
            {
                this.originalEstimate = value;
                this.NotifyOfPropertyChange(() => this.OriginalEstimate);
            }
        }

        /// <summary>
        /// Gets or sets the current estimate.
        /// </summary>
        /// <value>
        /// The current estimate.
        /// </value>
        public TimeSpan? CurrentEstimate
        {
            get
            {
                return this.currentEstimate;
            }

            set
            {
                this.currentEstimate = value;
                this.NotifyOfPropertyChange(() => this.CurrentEstimate);
                this.NotifyOfPropertyChange(() => this.PercentageComplete);
            }
        }

        /// <summary>
        /// Gets or sets the current time.
        /// </summary>
        /// <value>
        /// The current time.
        /// </value>
        public TimeSpan? ElapsedTime
        {
            get
            {
                return this.elapsedTime;
            }

            set
            {
                this.elapsedTime = value;
                this.NotifyOfPropertyChange(() => this.ElapsedTime);
                this.NotifyOfPropertyChange(() => this.PercentageComplete);
            }
        }

        public double? PercentageComplete
        {
            get
            {
                if (this.ElapsedTime == null || this.CurrentEstimate == null)
                {
                    return null;
                }
                
                if (this.CurrentEstimate.Value.Ticks == 0)
                {
                    return null;
                }

                var progressRatio = this.ElapsedTime.Value.Ticks / (double)this.CurrentEstimate.Value.Ticks;
                return progressRatio * 100.0;
            }
        }
    }
}